package model

import (
	"errors"
	"fmt"
	"github.com/hwcer/updater"
	"github.com/hwcer/updater/dataset"
	"server/share/config"
)

const roleGoodsField = "goods"
const roleGoodsFormat = "goods.%d"

type Goods struct {
}

func (this *Goods) Getter(u *updater.Updater, values *dataset.Values, keys []int32) error {
	//内存模式只会拉所有
	if len(keys) > 0 {
		return errors.New("record getter 参数keys应该为空")
	}
	doc := u.Handle(config.ITypeRole).(*updater.Document)
	role := doc.Any().(*Role)
	if role.Goods == nil {
		values.Reset(make(map[int32]int64))
	} else {
		values.Reset(role.Goods)
	}
	return nil
}

func (this *Goods) Setter(u *updater.Updater, values dataset.Data) error {
	doc := u.Handle(config.ITypeRole).(*updater.Document)
	role := doc.Any().(*Role)
	if role.Goods == nil {
		data := u.Handle(config.ITypeGoods).(*updater.Values).All()
		role.Goods = data
		doc.Dirty(roleGoodsField, data)
		return nil
	}
	for k, v := range values {
		field := fmt.Sprintf(roleGoodsFormat, k)
		doc.Dirty(field, v)
	}
	return nil
}
